<?php

/**
 * Created by PhpStorm.
 * User: shwy
 * Date: 2017/9/8
 * Time: 9:40
 * 审核管理相关接口
 */
class check extends CI_Controller
{


    public function __construct()
    {
        parent::__construct();
        $this->load->model('check_model');
//        $this->load->model('Checklog_model','checkLog');
        $this->load->model('procurement_model');
        $this->load->model('returnOrder_model');
        $this->load->model('purchaseOrder_model');


        $this->load->model('goldCheck_model');
        $this->load->model('biddingcheck_model');

        $this->load->model('biddingconfirm_model');
        $this->load->model('stockPut_model');
        $this->load->model('stockOut_model');
    }


    public function search()
    {

        $argc = [];
        if (isset($_GET['startTime']) && $_GET['startTime']) {
            $argc += [
                'createTime >' => strtotime($_GET['startTime']),
            ];
        }

        if (isset($_GET['endTime']) && $_GET['endTime']) {
            $argc += [
                'createTime <' => strtotime($_GET['endTime']),
            ];
        }

        if (isset($_GET['status']) && $_GET['status'] != '' && $_GET['status'] != 'null') {
            $argc += [
                'status ' => $_GET['status']
            ];

        }

        if (isset($_GET['search']) && $_GET['search'] != '') {
            $searchArg = [
                'field' => 'name',
                'arg' => $_GET['search'],
                'side' => 'both'
            ];
        } else {
            $searchArg = '';
        }


        $page_num = $this->config->item('fenye')['pageNum']; //每页的数据
        $start_position = ''; //初始数据
        $page = ($this->input->get('page')); //获取页码数量
        if (!$page) {
            $num = 1;
            $start_position = ($num - 1) * $page_num;
        } else {
            $start_position = ($page - 1) * $page_num;
        }

        if (!$argc) {
            $argc = [];
        }




//        print_r($argc);
//        die;

        //没有搜索的情况下
        $_GET['type'] = isset($_GET['type']) ? $_GET['type'] : 1;
        if ($_GET['type'] == 1) {
            $argc += [
                'type' => 1,
                //更换成动态名称
                'checker' =>  $_COOKIE['nickname']
//                    'checker' => '张总'
            ];

            if ($searchArg) {
                $data = $this->check_model->search('*', $argc, $searchArg, $page_num, $start_position);
            } else {
                $data = $this->check_model->page('check', $argc, $page_num, $start_position);
            }


            if ($data[0]) {



                foreach ($data[0] as $k => $v) {
                    if ($v['checkOrderType'] == '普通需求单' || $v['checkOrderType'] == '采购项目单' || $v['checkOrderType'] == '招投标项目需求单') {
                        $arg = [
                            'id' => $data[0][$k]['relevanceId']
                        ];
                        $field = 'id,needCode,type,name,createTime,source,contactMan';
                        $procurementData[] = $this->procurement_model->getByFieldQueryData($field, $arg, 0);
                    }
                    if ($v['checkOrderType'] == '采购退货单' || $v['checkOrderType'] == '采购换货单') {
                        $arg2 = [
                            'id' => $data[0][$k]['relevanceId']
                        ];
                        $field2 = 'id,code,type,name,createTime,source';
                        $returnOrderData[] = $this->returnOrder_model->getByFieldQueryData($field2, $arg2, 0);
                    }
                    if ($v['checkOrderType'] == '采购进货单') {
                        $arg3 = [
                            'id' => $data[0][$k]['relevanceId']
                        ];
                        $field3 = 'id,purchaseCode,type,name,createTime,source,createMan';
                        $purchaseOrderData[] = $this->purchaseOrder_model->getByFieldQueryData($field3, $arg3, 0);
                    }
                    if ($v['checkOrderType'] == '商品出库单') {
                        $arg4 = [
                            'id' => $data[0][$k]['relevanceId']
                        ];
                        $field4 = 'id,stockOutCode,type,createTime,createMan,name,source,createMan';
                        $stockOutData[] = $this->stockOut_model->getByFieldQueryData($field4, $arg4, 0);
                    }
                    if ($v['checkOrderType'] == '商品入库单') {
                        $arg5 = [
                            'id' => $data[0][$k]['relevanceId']
                        ];
                        $field5 = 'id,stockPutCode,type,createTime,createMan,name,source,createMan';
                        $stockPutData[] = $this->stockPut_model->getByFieldQueryData($field5, $arg5, 0);
                    }
                }



//                    echo '<pre>';
//                    __format($data[0]);
//                    print_r($procurementData);
//                    print_r($returnOrderData);
//                    print_r($purchaseOrderData);
//                    print_r($stockOutData);
//                    print_r($stockPutData);
//                    die;

//                __format($stockOutData);

                //条件判断审核表中的checkOrderType和各类表的中的type一直，因为有时候会出现id相同的情况

                //这里判断  $变量名[0]  第一个key0的是因为,此时就选没有数据,走到最后也会生成一个数组,而数组的key就是0起始.
                if(isset($procurementData) && $procurementData[0]){
                    foreach ($data[0] as $k2 => $v2) {
                        foreach ($procurementData as $k3 => $v3 ) {
                            if (empty($v3)) {
                                unset($procurementData[$k3]);
                                unset($data[0][$k2]);
                                continue;
                            }

                            //todo 数组偏移量 Undefined offset: 0
                            if($v2['relevanceId'] == $v3['id'] && $v2['checkOrderType'] == $v3['type']){
                                @$procurementData[$k3]['checkId'] = $data[0][$k2]['id'];
                                @$procurementData[$k3]['checker'] = $data[0][$k2]['checker'];
                                @$procurementData[$k3]['status'] = $data[0][$k2]['status'];
                                @$procurementData[$k3]['checkStatus'] = $data[0][$k2]['checkStatus'];
                                break;

                            }


                        }
                    }
                    $a[] = $procurementData;
                }



                if(isset($returnOrderData) && $returnOrderData[0]){
                    foreach ($data[0] as $k5 => $v5) {
                        foreach ($returnOrderData as $k4 => $v4) {
                            if (empty($v4)) {
                                unset($returnOrderData[$k4]);
//                                unset($data[0][$k5]);
                                continue;
                            }
                            //todo 数组偏移量 Undefined offset: 0
                            if($v5['relevanceId'] == $v4['id'] && $v5['checkOrderType'] == $v4['type']) {

                                @$returnOrderData[$k4]['checkId'] = $data[0][$k5]['id'];
                                @$returnOrderData[$k4]['checker'] = $data[0][$k5]['checker'];
                                @$returnOrderData[$k4]['status'] = $data[0][$k5]['status'];
                                @$returnOrderData[$k4]['checkStatus'] = $data[0][$k5]['checkStatus'];
                                break;
                            }
                        }
                    }
                    $a[] = $returnOrderData;

                }



                if(isset($purchaseOrderData) && $purchaseOrderData[0]){
                    foreach ($data[0] as $k6 => $v6) {
                        foreach ($purchaseOrderData as $k7 => $v7) {
                            if (empty($v7)) {
                                unset($purchaseOrderData[$k7]);
                                unset($data[0][$k6]);
                                continue;
                            }
                            //todo 数组偏移量 Undefined offset: 0
                            if($v6['relevanceId'] == $v7['id'] && $v6['checkOrderType'] == $v7['type']) {

                                @$purchaseOrderData[$k7]['checkId'] = $data[0][$k6]['id'];
                                @$purchaseOrderData[$k7]['checker'] = $data[0][$k6]['checker'];
                                @$purchaseOrderData[$k7]['status'] = $data[0][$k6]['status'];
                                @$purchaseOrderData[$k7]['checkStatus'] = $data[0][$k6]['checkStatus'];
                                break;

                            }
                        }
                    }

                    $a[] = $purchaseOrderData;
                }



//               __format($stockOutData);
//                __format($data[0]);

//                echo '<pre>';
//                print_r($stockOutData);
//                print_r($data[0]);
//                die;
                if(isset($stockOutData) && $stockOutData[0]){
                    foreach ($data[0] as $k8 => $v8) {
                        foreach ($stockOutData as $k9 => $v9) {
                            if (empty($v9)) {
                                unset($stockOutData[$k9]);
                                unset($data[0][$k8]);
                                continue;
                            }
                            //todo 数组偏移量 Undefined offset: 0
//                            __logInfo('relevanceId',$v8['relevanceId']);
//                            __logInfo('id',$v9['id']);
//                            __logInfo('checkOrderType',$v8['checkOrderType']);
//                            __logInfo('type',$v9['type']);

                            if($v8['relevanceId'] == $v9['id'] && $v8['checkOrderType'] == $v9['type']) {

                                @$stockOutData[$k9]['checkId'] = $data[0][$k8]['id'];
                                @$stockOutData[$k9]['checker'] = $data[0][$k8]['checker'];
                                @$stockOutData[$k9]['status'] = $data[0][$k8]['status'];
                                @$stockOutData[$k9]['checkStatus'] = $data[0][$k8]['checkStatus'];
                                break;

                            }
                        }
                    }

                    $a[] = $stockOutData;
                }



                if(isset($stockPutData) && $stockPutData[0]){
                    foreach ($data[0] as $k10 => $v10) {
                        foreach ($stockPutData as $k11 => $v11) {
                            if (empty($v11)) {
                                unset($stockPutData[$k11]);
                                unset($data[0][$k10]);
                                continue;
                            }
                            //todo 数组偏移量 Undefined offset: 0
                            if($v10['relevanceId'] == $v11['id'] && $v10['checkOrderType'] == $v11['type']) {

                                @$stockPutData[$k11]['checkId'] = $data[0][$k10]['id'];
                                @$stockPutData[$k11]['checker'] = $data[0][$k10]['checker'];
                                @$stockPutData[$k11]['status'] = $data[0][$k10]['status'];
                                @$stockPutData[$k11]['checkStatus'] = $data[0][$k10]['checkStatus'];
                                break;

                            }
                        }
                    }

                    $a[] = $stockPutData;
                }



                foreach ($a as $k8 => $v8) {
                    foreach ($v8 as $k9 => $v9) {
                        $arr[0][] = $v9;
                    }
                }

//                __format($arr);
                foreach ($arr[0] as $k10=>$v10){
                    $arr[0][$k10]['createTime'] = date('Y-m-d',$v10['createTime']);
                }

//                __format($arr);

//                    echo '<pre>';
//                    print_r($arr);
//                    die;

//                $viewData = [
//                    'list' => $arr,  //这里得到从数据库中的当页数据
//                    'totalNum' => $data['total_num'], //这里得到从数据库中的总页数
//                    'pageNum' => $page_num //每页显示多少条
//
//                ];
//
//                $this->load->view('admin/check/list', $viewData);

                $arr['pageNum'] = $page_num;
                $arr['this_page'] = $page;
                $arr['totalNum'] = $data['total_num'];
                $arr['type'] = 1;


                echo json_encode($arr, JSON_UNESCAPED_UNICODE);

            } else {

                $arr = [];

                echo json_encode($arr, JSON_UNESCAPED_UNICODE);
            }


//                print_r($viewData);
//                die();

        } elseif ($_GET['type'] == 2) {
            $argc += [
                'type' => 2,
                //更换成动态名称
                'checker' =>  $_COOKIE['nickname'],
                'checkOrderType' => '招标项目评审'
                // 'checker' => '曾健健'
            ];

            if ($searchArg) {
                $data = $this->check_model->search('*', $argc, $searchArg, $page_num, $start_position);
            } else {
                $data = $this->check_model->page('check', $argc, $page_num, $start_position);
            }

//                echo '<pre>';
//                print_r($data);
//                die;

            //查询管理的标书审核id
            if($data[0]){
                foreach ($data[0] as $k=>$v){
                    $arg = [
                        'id' => $v['relevanceId']
                    ];
                    $field = 'id,code,name,createTime,needAmount,address';
                    $biddingCheck[] = $this->biddingcheck_model->getByFieldQueryData($field, $arg, 0);

                }

            }




            foreach ($data[0] as $k2 => $v2) {
                if(isset($biddingCheck) && $biddingCheck){
                    foreach ($biddingCheck as $k3 => $v3) {
                        if ($v2['relevanceId'] == $v3['id']) {
                            $biddingCheck[$k3]['status'] = $v2['status'];
                            $biddingCheck[$k3]['checkId'] = $v2['id'];
                        }
                    }
                }

            }


            $data[0] = $biddingCheck;

            foreach ($data[0] as $k3=>$v4){
                $data[0][$k3]['createTime'] = date('Y-m-d',$v3['createTime']);
            }

//            echo '<pre>';
//                print_r($data);
//                die();
//

            $data['pageNum'] = $page_num;
            $data['this_page'] = $page;
            $arr['type'] = 2;



            echo json_encode($data, JSON_UNESCAPED_UNICODE);


        }else{
            //定标列表
            $argc += [
                'type' => 3,
                //更换成动态名称
                'checker' =>  $_COOKIE['nickname'],
                'checkOrderType' => '招标项目定标'
                // 'checker' => '曾健健'
            ];


            if ($searchArg) {
                $data = $this->check_model->search('*', $argc, $searchArg, $page_num, $start_position);
            } else {
                $data = $this->check_model->page('check', $argc, $page_num, $start_position);
            }

//                echo '<pre>';
//                print_r($data);
//                die;

            //查询管理的标书审核id
            if($data[0]){
                foreach ($data[0] as $k=>$v){
                    $arg2 = [
                        'relevanceId' => $v['relevanceId']
                    ];
                    $field = '*';
                    $confirmCheck[] = $this->biddingconfirm_model->getByFieldQueryData($field, $arg2, 0);


                }

            }


//            foreach ($data[0] as $k2 => $v2) {
//                if(isset($biddingCheck) && $biddingCheck){
//                    foreach ($biddingCheck as $k3 => $v3) {
//                        if ($v2['relevanceId'] == $v3['id']) {
//                            $biddingCheck[$k3]['status'] = $v2['status'];
//                            $biddingCheck[$k3]['checkId'] = $v2['id'];
//                        }
//                    }
//                }
//
//            }
//
//            if(isset($biddingCheck)){
//                $b[] = $biddingCheck;
//
//            }


            foreach ($data[0] as  $v) {
                if(isset($confirmCheck) && $confirmCheck){
                    foreach ($confirmCheck as $k2 => $v2) {
                        if ($v['relevanceId'] == $v2['relevanceId']) {
                            $confirmCheck[$k2]['status'] = $v['status'];
                            $confirmCheck[$k2]['checkId'] = $v['id'];
                            $confirmCheck[$k2]['isType'] = '招标项目定标';
                        }
                    }
                }

            }

//            if(isset($confirmCheck)){
//                $b[] = $confirmCheck;
//
//            }



//                echo '<pre>';
//                print_r($b);
//                die();

//            if(isset($b)){
//                foreach ($b as $k8 => $v8) {
//                    foreach ($v8 as $k9 => $v9) {
//                        $arr[] = $v9;
//                    }
//                }
//            }else{
//                $arr = [];
//            }

            if($confirmCheck){
                $data[0] = $confirmCheck;
            }else{
                $data[0] = [];
            }

            if($data[0]){
                foreach ($data[0] as $k3=>$v3){
                    $data[0][$k3]['createTime'] = date('Y-m-d',$v3['createTime']);
                }
            }



            $data['pageNum'] = $page_num;
            $data['this_page'] = $page;


            echo json_encode($data, JSON_UNESCAPED_UNICODE);


        }

    }
}










































